<template>
  <j-modal
    :title="title"
    :width="600"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="close"
    cancelText="关闭"
  >
    <a-form-model ref="form" :rules="validatorRules" :model="form" v-bind="formItemLayout">
      <a-form-model-item label="网格名称" prop="departName">
        <a-input v-model="form.departName" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="网格编号" v-if="form && Object.keys(form).length>0 && !form.id" prop="departCode">
        <a-input v-model="form.departCode" placeholder="请输入" />
      </a-form-model-item>
    </a-form-model>
    <template slot="footer">
      <a-button key="back" @click="close">取消</a-button>
      <a-button key="submit" type="primary" @click="handleOk">确定</a-button>
    </template>
  </j-modal>
</template>

<script>
import * as structureApi from '@/api/xlgc/structure.js'
export default {
  name: 'GroupsHandkeModal',
  props: {
    title: {
      type: String,
      default: '创建网格'
    }
  },
  data() {
    return {
      visible: false,
      disableSubmit: false,
      form: {},
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        departName: [
          { required: true, message: '请输入网格名称' }
        ],
        departCode: [
          { required: true, message: '请输入网格编号' },
          { pattern: /^[a-zA-Z0-9_]{0,}$/, message: '网格编号不能包有中文或者特殊字符' }
        ]
      }
    }
  },
  methods: {
    // 新建网格、新建下级网格
    add(record) {
      this.visible = true
      if (record && Object.keys(record).length > 0) {
        this.$set(this.form, 'parentId', record.value)
      } else {
        this.$set(this.form, 'parentId', '')
      }
      this.$nextTick(() => {})
    },
    // 修改网格
    edit(record) {
      this.visible = true
      this.$set(this.form, 'id', record.value)
      this.$set(this.form, 'departName', record.title)
      this.$nextTick(() => {})
    },
    close() {
      this.$emit('close')
      this.form = {}
      this.visible = false
    },
    handleOk() {
      let item = Object.assign({}, this.form)
      this.$refs.form.validate(valid => {
        if (valid) {
          let api, status
          // 新建网格、新建下级网格
          if (item && Object.keys(item).length > 0 && !item.id) {
            api = structureApi.addWG(item)
            status = 'add'
          // 修改网格
          } else if (item && Object.keys(item).length > 0 && item.id) {
            api = structureApi.updateWG(item)
            status = 'edit'
          }
          api.then(res => {
            if (res.success) {
              this.$message.success(res.message ? res.message : '操作成功！')
              this.close()
              this.$emit('ok', status)
            } else {
              this.$message.warn(res.message ? res.message : '操作失败！')
            }
          }).catch(e => {
            this.$message.warn('操作失败！')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
